using System;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 转盘界面
/// </summary>
public class TurntablePanel : MonoBehaviour
{
    [SerializeField] TurntableGrid mTurntableGrid;
    [SerializeField] private InputField mSelectedNumInputField;
    [SerializeField] private InputField mTurntableSizeInputField;
    [SerializeField] private Button mBeginBtn;
    
    
    void Start()
    {
        mBeginBtn.onClick.AddListener(BeginPlay);
        // mTurntableSizeInputField.onEndEdit.AddListener((str) =>
        // {
        //     int res = 5;
        //     int.TryParse(str, out res);
        //     TurntableController.Instance.GridNum = res;
        //     
        //     mTurntableGrid.SetTurntableGrid(TurntableController.Instance.GridNum);
        // });
        
        mTurntableGrid.SetTurntableGrid(TurntableController.Instance.GridNum);
    }

    private void BeginPlay()
    {
        int maxNum = mTurntableGrid.SideItemsCount;
        int tarNum = -1;
        try
        {
            tarNum = int.Parse(mSelectedNumInputField.text);
        }
        catch (Exception e)
        {
            Debug.LogError($"输入数字错误: {e}");
            //TODO: 可以考虑给个UI提示
        }

        if (tarNum > 0 && tarNum <= maxNum)
        {
            mBeginBtn.interactable = false;
            mTurntableGrid.StartPlay(tarNum, () => mBeginBtn.interactable = true);
        }
        else
        {
            Debug.Log($"当前数字为:{tarNum}, 输入的数字不能小于0, 也不能大于{maxNum}");
        }
    }
}
